<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-11-24
 * Time: 18:46
 */

namespace app\common\model;


use think\Model;
use think\Db;

class Enterprise extends Model
{
    /**
     * 个人认证的列表
     */
    public static function enterpriseList($map = [], $page)
    {
        return Db::name('enterprise_real_name')->alias('ern')
            ->join('user', 'ern_uid = u_id', 'left')
            ->where($map)
            ->field('ern.*, u_phone, u_email, u_nickname')
            ->order('ern_id DESC')
            ->paginate(15,false, ['page' => $page]);
    }
    /**
     * 认证通过  不通过
     */
    public static function edit($data)
    {
        $data['ern_update_time'] = time();
        $uid = $data['ern_uid'];
        if ($data['ern_status'] == 2) {
            $u_integral = Db::name('systems')->where('s_id', 1)->value('s_enterprise_auth');
            Db::startTrans();
            try{
                Db::name('enterprise_real_name')->where('ern_id', $data['ern_id'])->update($data);
                Db::name('user')->where('u_id', $uid)->setInc('u_integral',$u_integral);
                $title   = '企业认证成功';
                $content = '恭喜您，企业认证成功。获得'.$u_integral.'积分。';
                Messagem::addUserMessage($uid, $title, $content,2);
                Db::commit();
                return true;
            } catch(\Exception $e) {
                Db::rollback();
                return false;
            }
        } elseif ($data['ern_status'] == 3) {
            Db::name('enterprise_real_name')->where('ern_id', $data['ern_id'])->delete();
            $title   = '企业认证失败';
            $content = '很遗憾，您的企业认证失败。未能获得积分。请您重新提交您的信息进行认证';
            Messagem::addUserMessage($uid, $title, $content, 4);
            Db::commit();
            return true;
        } else {
            return false;
        }
    }

}